package pdm.yamba;

import pdm.yamba.dto.Status;
import pdm.yamba.listener.IStatusPublishListener;
import pdm.yamba.service.StatusUploadService;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class StatusActivity extends BaseActivity {
	
	int statusLength;
	//boolean enableButton = true;
	String _currentStateButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_status);
		
		_currentStateButton = getComponentName().flattenToString();		
		statusLength = app.getStatusLength();
		
		// set the configured status message length
		TextView txtCharCount = (TextView) findViewById(R.id.txtCharCount);
		txtCharCount.setText(String.valueOf(statusLength));

		Button updateBtn = (Button)this.findViewById(R.id.btnUpdate);
		updateBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {		
				
				// retrieve the status message
				EditText messageTxt = (EditText)StatusActivity.this.findViewById(R.id.etxtMessage);
				String message = messageTxt.getText().toString();
				
				// Create the Status object
				Status status = new Status();
				status.setMessage(message);
				status.setAuthor(app.getUsername());
				
				//enableButton = false;
				//((Button)findViewById(R.id.btnUpdate)).setEnabled(enableButton);				
				
				// start Async Task that will publish the status
				//app.PublishStatus(status);
				
				Intent intent = new Intent(new Intent(StatusActivity.this, StatusUploadService.class));
				intent.putExtra("message",status.getMessage());
				startService(intent);

			}			
		});
		/*
		app.getTwitterFeed().addStatusPublishListener(new IStatusPublishListener(){

			@Override
			public void onPublishStart() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onPublishComplete(Status status) {
				enableButton = true;
				findViewById(R.id.btnUpdate).setEnabled(enableButton);
				
			}

			@Override
			public void onPublishProgress(Status status) {
				// TODO Auto-generated method stub
				
			}}); 
		*/
		EditText eTxtMessage = (EditText)this.findViewById(R.id.etxtMessage);
		// set max length
		eTxtMessage.setFilters( new InputFilter[] { new InputFilter.LengthFilter(statusLength) } );
		// event handler to control the remaining characters of the message
		eTxtMessage.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) { }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

            @Override
            public void afterTextChanged(Editable s) 
            {
            	EditText eTxtMessage = (EditText)findViewById(R.id.etxtMessage);
            	TextView txtCharCount = (TextView)findViewById(R.id.txtCharCount);            	
            	int count = statusLength - eTxtMessage.length();
            	txtCharCount.setText(String.valueOf(count));
            }
        });
	}
	
	@Override	
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		super.onCreateOptionsMenu(menu);
		
		MenuItem item = menu.findItem(R.id.action_status);
		item.setVisible(false);	
		
		MenuItem itemUpdate = menu.findItem(R.id.action_update);		
		itemUpdate.setVisible(false);
		
		return true;
	}
/*	
	@Override
	protected void onSaveInstanceState(Bundle outState) 
	{
		Log.v("Yamba", "onSaveInstanceState()");
		outState.putBoolean(_currentStateButton, enableButton);
		super.onSaveInstanceState(outState);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) 
	{
		super.onRestoreInstanceState(savedInstanceState);
		Log.v("Yamba", "onRestoreInstanceState() ");
		enableButton = savedInstanceState.getBoolean(_currentStateButton);
		Button button = (Button) findViewById(R.id.btnUpdate);
		button.setEnabled(enableButton);
	}
*/	
}
